// 页面form 与 接口data 数据结构互相转换
import {separator, imageDomain} from '@/common/js/resource.js';
import tool from '@/common/js/tools.js';
// 供应商
export default {
	/**
	 * 将接口返回的res.data转换成页面的formData
	 * @param {Object} data 接口返回的data
	 * @return {Object} 页面formData
	 */
	newForm(data){
		return {
			id: data.GongYingShangID || data.ID,
			companyId: data.QiYeID,
			name: data.GongYingShangMingCheng, // 供应商名称
			type: data.GongYingLeiBie, // 供应商类别
			address: data.DiZhi, // 地址
			contacts: data.LianXiRen, // 联系人
			mobile: data.LianXiDianHua, // 电话
			usci: data.SheHuiXinYongDaiMa, // 统一社会信用代码18位
			images1: tool.addImageDomain(data.XuKeZhengUrl), // 许可证
			images2: tool.addImageDomain(data.YingYeZhiZhaoUrl), // 营业执照
			status: data.ZhuangTai, // 状态
			_add_time_str: data.GengXinDate, // 维护日期
		}
	},
	/**
	 * 将接口返回的res.data转换成页面的dataList
	 * @param {Array} list 接口返回的list
	 * @return {Object} 页面dataList
	 */
	newFormList(list){
		/*
		DiZhi: "太白山路88号"
		GongYingLeiBie: "1,2,3,4,5"
		GongYingShangID: 1              "ID":1,
		GongYingShangMingCheng: "小王粮油店"
		LianXiDianHua: "15040121321"
		LianXiRen: "黄蓉"
		QiYeID: 1
		Qu: null
		SheHuiXinYongDaiMa: "111111111111111111"
		Sheng: null
		Shi: null
		ZhuangTai: 1
		*/
		let arr = list.map(data => {
			return {
				id: data.GongYingShangID || data.ID,
				companyId: data.QiYeID,
				name: data.GongYingShangMingCheng, // 供应商名称
				type: data.GongYingLeiBie, // 供应商类别
				address: data.DiZhi, // 地址
				contacts: data.LianXiRen, // 联系人
				mobile: data.LianXiDianHua, // 联系电话
				usci: data.SheHuiXinYongDaiMa, // 统一社会信用代码18位
				images1: tool.addImageDomain(data.XuKeZhengUrl), // 许可证
				images2: tool.addImageDomain(data.YingYeZhiZhaoUrl), // 营业执照
				status: data.ZhuangTai, // 状态
				_add_time_str: data.GengXinDate, // 维护日期
			}
		})
		return arr;
	},
	/**
	 * 将页面页面的formData转换成接口需要的data
	 * @param {Object} form 页面formData
	 * @return {Object} 传给接口的data
	 */
	newApiData(data){
		return {
			Id: data.id || '',
			GongYingShangID: data.id || '',
			QiYeID: data.companyId,
			GongYingShangMingCheng: data.name, // 姓名
			DiZhi: data.address,
			LianXiRen: data.contacts,
			LianXiDianHua: data.mobile,
			GongYingLeiBie: data.type,
			XuKeZhengUrl: data.images1.replaceAll(imageDomain, ''),
			YingYeZhiZhaoUrl: data.images2.replaceAll(imageDomain, ''),
			GengXinDate: data._add_time_str
		}
	},
	// 比较新form和原form是否相同（无变化）
	compare(form1, form2){
		return form1.id            == form2.id           
			&& form1.companyId     == form2.companyId    
			&& form1.name          == form2.name         
			&& form1.type          == form2.type
			&& form1.address       == form2.address        
			&& form1.contacts      == form2.contacts          
			&& form1.mobile        == form2.mobile       
			&& form1.usci          == form2.usci  
			&& form1.images1       == form2.images1
			&& form1.images2       == form2.images2 
			&& form1.status        == form2.status 
			&& form1._add_time_str == form2._add_time_str
	},
}